/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.NursingService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.NursingLevel;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(NursingLevel.class)
@Component
public class NursingLevelRepository extends BaseRepository<NursingLevel> {

	@Autowired
	private NursingService nursingService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(NursingLevel.class);
	}

	@Override
	@JsonApiSave
	public NursingLevel save(NursingLevel level) {
		if (level.getId() == null) {
			NursingLevel temp = nursingService.getNursingLevelByName(level.getName());
			if (temp == null) {
				level = super.save(level);
			} else {
				level.setErrcode(4);// 名字重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (level.getDelStatus() == 1) {// 删除
				int deleteFlag = nursingService.checkLevelRelations(level);
				if(deleteFlag>0){
					level.setErrcode(8);// no delete  
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NO_DELETE.label(), null);
				}else{
					dslJpaService.update(level);
				}
				// nursingService.deleteNursrGroup(level.getId());
			} else {//
				int updateFlag = 1;
				NursingLevel oldData = nursingService.getNursingLevelById(level.getId());
				if (oldData.getName().equals(level.getName())) {

				} else {
					NursingLevel temp = nursingService.getNursingLevelByName(level.getName());
					if (temp == null) {

					} else {
						updateFlag = 0;
					}
				}

				if (updateFlag == 1) {
					level = super.save(level);
				} else {
					level.setErrcode(4);// 名字重复
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return level;
	}

	
	@Override
	@JsonApiFindAll
	public Iterable<NursingLevel> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);
		if (keyMap.containsKey("queryTenant")) {
			List dataList = new ArrayList<>();
			dataList = nursingService.getTenantNursingLevel(keyMap.get("nameLike"),keyMap.get("typecode"));
			commonService.setPageInfo(dataList, requestParams);
			dataList = commonService.getPageData(dataList, requestParams);
			return dataList;
		}else{
			return super.findAll(requestParams);
		}

	}
	
}
